package com.withouther.robot.rainingrobot.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @ClassName ByteUtil
 * @Auther: tzq
 * @Date: 2020/7/1 14:39
 * @Description:
 */
public class ByteUtil {
    /**
     * int到byte[]
     * @param i
     * @return
     */
    public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        // 由高位到低位
        result[3] = (byte) ((i >> 24) & 0xFF);
        result[2] = (byte) ((i >> 16) & 0xFF);
        result[1] = (byte) ((i >> 8) & 0xFF);
        result[0] = (byte) (i & 0xFF);
        return result;
    }

    /**
     * byte[]转int
     * @param bytes
     * @return
     */
    public static int byteArrayToInt(byte[] bytes) {
        return bytes[0] & 0xFF | //
                (bytes[1] & 0xFF) << 8 | //
                (bytes[2] & 0xFF) << 16 | //
                (bytes[3] & 0xFF) << 24; //
//        int value = 0;
//        // 由高位到低位
//        for (int i = 0; i < 4; i++) {
//            int shift = (4 - 1 - i) * 8;
//            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
//        }
//        return value;
    }

    public static void copy (byte[] original, int start, byte[] news, int end, int length) {
        for (int i = start; i < length; i++) {
            if (i > end) {
                break;
            }
            news[i] = original[i];
        }
    }

    public static void copy(byte[] original, byte[] news, int length) {
        for (int i = 0; i < length; i++) {
            news[i] = original[i];
        }
    }
    /// 根据二进制数据大小计算对应的同等语音长度
    /// sampleRate 仅支持8000或16000
    public static int getSleepDelta(int dataSize, int sampleRate) {
        // 仅支持16位采样
        int sampleBytes = 16;
        // 仅支持单通道
        int soundChannel = 1;
        return (dataSize * 10 * 8000) / (160 * sampleRate);
    }
    public static void main(String[] args) {
//        String byteLength = "";
//        System.out.println(Integer.parseInt(byteLength, 0));
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\IDEAProject\\ics-robot\\target\\robot-0.0.1.jar.original"));
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bs = new byte[0];
//        String id = "123123";
//        String type = "12312223";
//        MapCache mapCache = new MapCache();
//        mapCache.add(id,type);
//        try {
//            Thread.sleep(1000);
//            System.out.println(mapCache.get(id));
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println(getSleepDelta(2048, 8000));
//        byte byetes = 2;
//        int abc = byetes;
//        System.out.println(abc);
//        System.out.println(new Date(1593829477));
//        System.out.println(UUID.fastUUID());
    }
}
